/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.wx.entity.WxBusiness;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.wx.entity.WxBusinessApply;
import com.jeesite.modules.wx.service.WxBusinessApplyService;

import java.util.List;

/**
 * wx_business_applyController
 * @author wangjianxin
 * @version 2018-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxBusinessApply")
public class WxBusinessApplyController extends BaseController {

	@Autowired
	private WxBusinessApplyService wxBusinessApplyService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public WxBusinessApply get(String id, boolean isNewRecord) {
		return wxBusinessApplyService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("wx:wxBusinessApply:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxBusinessApply wxBusinessApply, Model model) {
		model.addAttribute("wxBusinessApply", wxBusinessApply);
		return "modules/wx/wxBusinessApplyList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("wx:wxBusinessApply:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WxBusinessApply> listData(WxBusinessApply wxBusinessApply, HttpServletRequest request, HttpServletResponse response) {
		Page<WxBusinessApply> page = wxBusinessApplyService.findPage(new Page<WxBusinessApply>(request, response), wxBusinessApply);

		List<WxBusinessApply> wxBusinessApplyList = page.getList();
		if(CollectionUtils.isNotEmpty(wxBusinessApplyList)){
			for(WxBusinessApply temp : wxBusinessApplyList){
				Long isRelation = temp.getIsRelation();
				if(isRelation.intValue() == 1){
					temp.setIsRelationText("已联系");
				}else if(isRelation.intValue() == 0){
					temp.setIsRelationText("未联系");
				}
			}
		}

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("wx:wxBusinessApply:view")
	@RequestMapping(value = "form")
	public String form(WxBusinessApply wxBusinessApply, Model model) {
		model.addAttribute("wxBusinessApply", wxBusinessApply);
		return "modules/wx/wxBusinessApplyForm";
	}

	/**
	 * 保存wx_business_apply
	 */
	@RequiresPermissions("wx:wxBusinessApply:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated WxBusinessApply wxBusinessApply) {
		wxBusinessApplyService.save(wxBusinessApply);
		return renderResult(Global.TRUE, text("修改申请商家状态成功！"));
	}
	
	/**
	 * 删除wx_business_apply
	 */
	@RequiresPermissions("wx:wxBusinessApply:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WxBusinessApply wxBusinessApply) {
		wxBusinessApplyService.delete(wxBusinessApply);
		return renderResult(Global.TRUE, text("删除申请商家成功！"));
	}
	
}